package com.geovis.emergency.spd.controller.app;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.geovis.common.auth.util.AuthUtil;
import com.geovis.common.core.api.Result;
import com.geovis.emergency.spd.biz.app.service.SptTfsjService;
import com.geovis.emergency.spd.biz.system.service.ISystemDeptService;
import com.geovis.emergency.spd.biz.system.service.ISystemFileBusinessService;
import com.geovis.emergency.spd.biz.system.service.ISystemUserService;
import com.geovis.emergency.spd.entity.app.dto.EventReportDto;
import com.geovis.emergency.spd.entity.system.constant.SystemFileBusinessConstant;
import com.geovis.emergency.spd.entity.system.entity.SystemDept;
import com.geovis.emergency.spd.entity.system.entity.SystemFileBusiness;
import com.geovis.emergency.spd.entity.system.entity.SystemUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 信息上报
 * </p>
 *
 * @author Lonsho
 * @since 2022-06-08
 */
@RestController
@RequestMapping("/app/event")
@RequiredArgsConstructor
@Api(value = "信息上报模块接口", tags = "信息上报模块接口")
@Slf4j
@Validated
public class EventController {

    private final SptTfsjService sptTfsjService;

    private final ISystemDeptService systemDeptService;

    private final ISystemUserService systemUserService;

    private final ISystemFileBusinessService systemFileBusinessService;

    @ApiOperation(value = "上报事件")
    @PostMapping("/reportEvent")
    public Result reportEvent(@RequestBody EventReportDto reportDto) {
        SystemUser sendUser=systemUserService.getById(AuthUtil.getCurrentUserId());
        SystemDept sendDept=systemDeptService.getById(sendUser.getDeptId());
        SystemDept receiveDept=systemDeptService.getById("1733385439521296386");
        if(reportDto.getBatchType().equals("2"))
        {
            if(!StringUtils.hasText(reportDto.getEventId()))
            {
                return Result.failed("续报类型，事件id不能为空");
            }
        }
        String eventId=sptTfsjService.reportEvent(sendUser,sendDept,receiveDept,reportDto);
        //保存附件
        if(!CollectionUtils.isEmpty(reportDto.getFileIds()))
        {
            reportDto.getFileIds().stream().forEach(fileId->{
                SystemFileBusiness sfb=new SystemFileBusiness(fileId, SystemFileBusinessConstant.APP_EVENT_REPORT_FILE,eventId);
                systemFileBusinessService.save(sfb);

            });
        }
        return Result.ok("成功");
    }

}
